<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2024/9/19
 * Time: 下午4:53
 */

namespace app\web\controller;

use app\api\controller\v2\Auth;
use app\common\cachemodel\ziwei\Zl9ZiweiUserConfig;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use app\common\lib\ziwei\Ziwei as ZiweiLib;
use app\common\model\Recode;
use think\Controller;
use think\Env;
use think\Exception;

/**
 * 紫微盘页面
 * Class Ziwei
 * @package app\web\controller
 */
class Ziwei extends Controller
{
    private $isTest = false;

    public function index()
    {
        $headers = [
            'app-type' => $this->request->header('app-type', 'h5'),
            'version' => $this->request->header('version'),
            'did' => $this->request->header('did'),
            'time' => $this->request->header('time'),
            'sign' => $this->request->header('sign'),
            'model' => $this->request->header('model'),
            'phone-version' => $this->request->header('phone-version'),
            'timezone' => $this->request->header('timezone', 8),
            'access-token' => $this->request->header('access-token'),
        ];
        $rid = input('rid/d', 0);
//        $rid = 4923326; // Seunk 【beta】
//        $rid = 33156521; // 施越凌 【beta】
//        $rid = 4923211; // 姜皓元 【beta】
//        $rid = 4921747; // 奇 【beta】
//        $rid = 4921761; // 鼎 【beta】
//        $rid = 4921746; // 黑 【beta】
//        $rid = 33156570; // 闰月 【beta】

        // 校验token
        try
        {
            $auth = new Auth();
            $auth->checkRequestAuth();
            $uid = $auth->user['id'];
        }
        catch (Exception $e)
        {
            // 用户未登录、没有档案、校验失败的情况
            if (!$this->isTest)
            {
                throw new ApiException('紫微权限认证失败,请稍后重试！');
            }
            $uid = 0;
        }
        $recordInfo = (new Recode())->getRecode(['id' => $rid]);
        if (!$recordInfo)
        {
            throw new ApiException('紫微获取档案信息错误');
        }
        // 获取用户的紫微设置
        $setting = (new Zl9ZiweiUserConfig())->getInfoById($uid);
        if (empty($setting))
        {
            $setting = ['config' => []];
        }

        $data = (new ZiweiLib())->setRecordInfo($recordInfo)->setSetting($setting['config'])->getChartInfo();

        $data['headers'] = json_encode($headers);
        $data['host'] = Env::get('host.domain');

        return $this->fetch('index', $data);
    }

    public function detail()
    {
        // 校验token
        try
        {
            $auth = new Auth();
            $auth->checkRequestAuth();
            $uid = $auth->user['id'];
        }
        catch (Exception $e)
        {
            // 用户未登录、没有档案、校验失败的情况
            if (!$this->isTest)
            {
                throw new ApiException('紫微权限认证失败,请稍后重试！');
            }
            $uid = 0;
        }

        $rid = input('rid/d', 0);

        $recordInfo = (new Recode())->getRecodeWithAstro(['id' => $rid]);
        if (!$recordInfo)
        {
            throw new ApiException('紫微获取档案信息错误');
        }
        $utilModel = new Util();
        $sun = mb_substr($utilModel->recodeAstro($recordInfo['sun']), 1, 1);
        $moon = mb_substr($utilModel->recodeAstro($recordInfo['moon']), 1, 1);
        $asc = mb_substr($utilModel->recodeAstro($recordInfo['asc']), 1, 1);
        $recordInfo['astro'] = '日' . $sun . '月' . $moon . '升' . $asc;
        $num2Text = [1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六', 7 => '七', 8 => '八', 9 => '九', 10 => '十', 11 => '十一', 12 => '十二'];
        $recordInfo['timezone_text'] = ($recordInfo['timezone'] >= 0 ? '东' : '西') . $num2Text[abs((int)$recordInfo['timezone'])] . '区';

        // 获取用户的紫微设置
        $setting = (new Zl9ZiweiUserConfig())->getInfoById($uid);
        if (empty($setting))
        {
            $setting = ['config' => []];
        }

        $data = (new ZiweiLib())->setRecordInfo($recordInfo)->setSetting($setting['config'])->getChartDetail();
        $data['record'] = $recordInfo;

        return $this->fetch('', $data);
    }

    public function setting()
    {
        if ($this->request->isPost())
        {
            try
            {
                $auth = new Auth();
                $auth->checkRequestAuth();
                $uid = $auth->user['id'];
            }
            catch (Exception $e)
            {
                // 用户未登录、没有档案、校验失败的情况
                if (!$this->isTest)
                {
                    throw new ApiException('紫微权限认证失败,请稍后重试！');
                }
                $uid = 0;
            }

            // 提交过来的设置，修改设置
            $param = $this->request->param();

            $setting = [
                'time_type' => (int)$param['time_type'],
                'time_start' => (int)$param['time_start'],
                'zihua' => (int)$param['zihua'],
                'sihua_jia' => (int)$param['sihua_jia'],
                'sihua_wu' => (int)$param['sihua_wu'],
                'sihua_geng' => (int)$param['sihua_geng'],
                'sihua_xin' => (int)$param['sihua_xin'],
                'sihua_ren' => (int)$param['sihua_ren'],
                'sihua_gui' => (int)$param['sihua_gui'],
                'runyue' => (int)$param['runyue'],
                'an_kuiyue' => (int)$param['an_kuiyue'],
            ];

            $res = (new Zl9ZiweiUserConfig())->SaveData($uid, $setting);

            if ($res)
            {
                $rid = input('rid/d', 0);
                $headers = [
                    'app-type' => $this->request->header('app-type', 'h5'),
                    'version' => $this->request->header('version'),
                    'did' => $this->request->header('did'),
                    'time' => $this->request->header('time'),
                    'sign' => $this->request->header('sign'),
                    'model' => $this->request->header('model'),
                    'phone-version' => $this->request->header('phone-version'),
                    'timezone' => $this->request->header('timezone', 8),
                    'access-token' => $this->request->header('access-token'),
                ];
                $recordInfo = (new Recode())->getRecode(['id' => $rid]);
                if (!$recordInfo)
                {
                    throw new ApiException('紫微获取档案信息错误');
                }
                // 获取用户的紫微设置
                $setting = (new Zl9ZiweiUserConfig())->getInfoById($uid);
                if (empty($setting))
                {
                    $setting = ['config' => []];
                }

                $data = (new ZiweiLib())->setRecordInfo($recordInfo)->setSetting($setting['config'])->getChartInfo();

                $data['headers'] = json_encode($headers);
                $data['host'] = Env::get('host.domain');

                return show(config('code.success'), '操作成功', $data);
            }
        }
        return show(config('code.error'), '设置错误');
    }
}
